﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using CC.Yacht.Utilities;

namespace CC.Yacht.Windows.Forms
{
    public partial class GameTablePanel : UserControl
    {
        #region Constructor
        public GameTablePanel()
        {
            InitializeComponent();
            _IsInitialized = true;
        }

        ~GameTablePanel()
        {
            Dispose();
        }
        #endregion

        #region Private Fields
        private readonly bool _IsInitialized;
        private SoundPlayer _SoundPlayer;
        #endregion

        #region Public Events
        public event EventHandler<DiceChangedEventArgs> DiceChanged;
        public event EventHandler Rolling;
        public event EventHandler Starting;
        #endregion

        #region Public Properties
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new BorderStyle BorderStyle
        {
            get { return _PanelDiceTable.BorderStyle; }
            set
            {
                if (!_IsInitialized)
                {
                    _PanelDiceTable.BorderStyle = value;
                }
            }
        }

        public bool CanLock
        {
            get { return _DiceControl.CanLock; }
            set { _DiceControl.CanLock = value; }
        }

        public bool CanRoll
        {
            get { return _ButtonRoll.Enabled; }
            set { _ButtonRoll.Enabled = value; }
        }

        public bool CanStart
        {
            get { return (_ButtonStart.Enabled && _ButtonStart.Visible); }
            set
            {
                _ButtonStart.Enabled = value;
                _ButtonStart.Visible = value;
            }
        }

        /*
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Size MaximumSize
        {
            get { return base.MinimumSize; }
            set
            {
                if (!_IsInitialized)
                {
                    base.MaximumSize = value;
                }
            }
        }
        */

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Size MinimumSize
        {
            get { return base.MinimumSize; }
            set
            {
                if (!_IsInitialized)
                {
                    base.MinimumSize = value;
                }
            }
        }

        /*
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Size Size
        {
            get { return base.Size; }
            set
            {
                if (!_IsInitialized)
                {
                    base.Size = value;
                }
            }
        }
        */
        #endregion

        #region Private Event Handlers
        private void _ButtonRoll_Click(object sender, EventArgs e)
        {
            OnRolling();
        }

        private void _ButtonStart_Click(object sender, EventArgs e)
        {
            _FireworksPanel.Text = string.Empty;
            _FireworksPanel.Stop();

            OnStarting();
        }

        private void _DiceControl_Changed(object sender, DiceChangedEventArgs e)
        {
            OnDiceChanged(e);
        }
        #endregion

        #region Private Methods
        private void PlaySound(Stream stream)
        {
            if (_SoundPlayer == null)
            {
                _SoundPlayer = new SoundPlayer(this);
            }

            _SoundPlayer.Play(stream);
        }
        #endregion

        #region Protected Methods
        protected void OnDiceChanged(DiceChangedEventArgs eventArgs)
        {
            if (DiceChanged != null)
            {
                DiceChanged(this, eventArgs);
            }
        }

        protected void OnRolling()
        {
            if (Rolling != null)
            {
                Rolling(this, EventArgs.Empty);
            }
        }

        protected void OnStarting()
        {
            if (Starting != null)
            {
                Starting(this, EventArgs.Empty);
            }
        }
        #endregion

        #region Public Methods
        public new void Dispose()
        {
            if (_SoundPlayer != null)
            {
                _SoundPlayer.Dispose();
                _SoundPlayer = null;
            }

            base.Dispose();
        }

        public void GameOver(User winner)
        {
            CanLock = false;
            CanRoll = false;
            
            _FireworksPanel.Text = winner.Username + " wins!";
            _FireworksPanel.Start();
        }

        public void Rolled(Dice dice)
        {
            PlaySound(Yacht.Properties.Resources.RollDice);

            for (int i = RandomHelper.Next(10, 20); i >= 0; i--)
            {
                _DiceControl.Roll(true);
            }

            UpdateDice(dice);
        }

        public void StopFireworks()
        {
            _FireworksPanel.Text = string.Empty;
            _FireworksPanel.Stop();
        }

        public void UpdateDice(Dice dice)
        {
            _DiceControl.UpdateDice(dice);
        }
        #endregion
    }
}
